package com.jie.staservice.controller;


import com.jie.commonutils.ResultEntity;
import com.jie.staservice.service.StatisticsDailyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 网站统计日数据 前端控制器
 * </p>
 *
 * @author xzj
 * @since 2020-10-18
 */
//@CrossOrigin  // 解决跨域问题
@RestController
@RequestMapping("/staservice/sta")
public class StatisticsDailyController {

    private final StatisticsDailyService staService;

    @Autowired
    public StatisticsDailyController(StatisticsDailyService staService) {
        this.staService = staService;
    }

    /**
     * 统计某一天的注册人数生成统计数据并加到数据库中
     * @param date  某一天
     * @return      返回方法是否调用成功
     */
    @PostMapping("/registerCount/{date}")
    public ResultEntity registerCount(
            @PathVariable("date") String date
    ) {
        staService.registerCount(date);
        return ResultEntity.ok();
    }

    /**
     * 图表显示, 返回日期json数组和数量json数组
     * @param type   数据类型
     * @param begin  开始日期
     * @param end    结束日期
     * @return       返回日期json数组和数量json数组
     */
    @GetMapping("/showData/{type}/{begin}/{end}")
    public ResultEntity showData(
            @PathVariable("type") String type,
            @PathVariable("begin") String begin,
            @PathVariable("end") String end
    ) {
        Map<String, Object> map = staService.getShowData(type, begin, end);
        return ResultEntity.ok().data(map);
    }

}

